/*
 * This is a collection of functions and objects to manage the shopping cart performance 
 * It use a Object Model we will call the the shopping cart object model. It is like this:
 * class (object)
 *
 * order (myOrder)
 * order::orderCart (myCart)
 * order::orderCart::array(orderItem) (items)
 */
 
 // myOrder is the global variable that contain the shoppping cart object model objects. Any time we need to know the shopping cart
 // state or to modify any data from it we will do it through this variable. We initialize this variable here.


// Equivalent to above
// Equivalent to orderItem




/* loadItemsListView
 * This function utility is to load the shopping cart prompt on the myOrder, myCart and the myCart's items objects.
 * Shopping cart prompt appears first time the page is showed, then we call this function tu capture it and save it at these variables for the following use
 * of the function refreshOrderView. The argument that receives is the id of the element that contains the shopping cart view.
 * We will save it at the variable/property named blankView at the correpondent object excepts for the orderItems objects that as they are not
 * instanciated from the beginning (only when products/items are added to the cart) we prefer to create a variable at the orderCart object named
 * myItemView for that purpose.
 * This function must be instanciated just once at the bottom of the index.php file when page is loading for first time.
 */



/* efreshItemsListView
 * This function utility is to generate the shopping cart view and to insert it inside the container web page element.
 * It use the shopping cart object model for doing so.
 * The argument that receives is the id of the element that contains the shopping cart view.
 * The generated filled view is saved at each object of the shopping cart object model at the property/variable myView
 * The myView variable at the root element myOrder we'll contain the entire view.
 * This function will be instantiated the first time we load the page to replace the template shopping cart view with
 * the empty shopping cart view and then any time that there are changes at the cart, that is, item/products insertion/extractions.
 */
 
 function orderNode() {
	itemsNode.call(this);
	this.subTotal=0;
}

orderNode.prototype=Object.create(itemsNode.prototype);

orderNode.prototype.constructor=orderNode;

orderNode.prototype.setFromArray=setFromArray;

	/*
	 * This function adds the item sent at the argument to the items array or increment the
	 * item if it is already in the list
	 */
	
	orderNode.prototype.addItem=function(item)	{
		//First we check if the product is already in the cart
		for (var i=0;i<this.items.length;i++) {
			if (this.items[i].id==item.id) {
				this.items[i].quantity += item.quantity;
				//We remove the item it its quantity is 0 or less
				if (this.items[i].quantity <= 0 ) {
					this.items.splice(i,1);
				}
				return true;
			}
		}
		//We have checked for every item and there is not, other case we would have exit the funtion. So we can add the item to the cart.
		var newItem=new cartItem();
		newItem.parentNode=this;
		newItem.setFromArray(item);
		this.items.push(newItem);
	}

	//this method calculate the cart price
	orderNode.prototype.sumPrice=function() {
		var total=0;
		for (var i=0;i<this.items.length;i++)	{
			total += this.items[i].price * this.items[i].quantity;
		}
		this.subTotal=total;
		return total;
	}
	orderNode.prototype.mySetOrderBox=function(elementId) {
		var myOrderPointer=document.getElementById(elementId);
		var myOrderElements=myOrderPointer.getElementsByTagName("*");
		for (var i in myOrderElements) {
			if (typeof myOrderElements[i].getAttribute != 'function') continue;
			// This serves to fulfill the serOrder input of the checkout form with the serialized order and perform the request when link "checkout"
			//  is clicked. The order submitting element, at the template, myFor attribute refers to the id name of the form element.
			if (myOrderElements[i].getAttribute("myProperty") == "checkout") { //we search for the checkout link
				myOrderElements[i].thisOrder=this;
				myOrderElements[i].onclick=function(){
					if (this.thisOrder.items.length==0) return false; // If cart is empty we deactivate it
					var myForm=document.getElementById(this.getAttribute("myFor"));
					var myInput=myForm.serOrder;
					var myOrderData=new orderNode();
					myOrderData.setFromArray(this.thisOrder);
					for (var i in this.thisOrder.items) {
						myOrderData.items[i]=new cartItem(); 
						myOrderData.items[i].setFromArray(this.thisOrder.items[i]);
					}						
					myInput.value=JSON.stringify(myOrderData);
					initLoadingElements(myForm);
					myForm.request();
					return false;
				}
			}
			
			// This serves to set the empty cart "button"
			if (myOrderElements[i].getAttribute("myProperty") == "remove") { //we search for the checkout link
				myOrderElements[i].thisOrder=this;
				myOrderElements[i].onclick=function(){
					// We empty the shopping cart
					this.thisOrder.items=new Array();
					this.thisOrder.sumPrice();
					this.thisOrder.refreshItemsListView(this.getAttribute("myFor"));
					return false;
				}
			}
		}
	};
orderNode.prototype.setView=setView;
// Equivalent to above function
orderNode.prototype.refreshItemsListView=function (elementId) {
	itemsNode.prototype.refreshItemsListView.call(this,elementId);

	var myItemsListContainerElement=document.getElementById(elementId);
	var myOrderContainerElement=document.getElementById(myItemsListContainerElement.getAttribute("myFor"));
	orderNode.prototype.setView.call(this,myOrderContainerElement);
};

/*
 * It defines the item object.
 */
function cartItem() {
	item.call(this);
	this.quantity=1;
}

cartItem.prototype=Object.create(item.prototype);

cartItem.prototype.constructor=cartItem;

	// This function generates the item view from the template item view
	cartItem.prototype.mySetView=function(myItemView) {
		item.prototype.mySetView.call(this,myItemView);
		this.myView=myItemView;
		// it adds the remove item facility at the element quantity of the shopping cart view
		myItemElements=this.myView.getElementsByTagName("*");
		for (var j in myItemElements) {
			if (typeof myItemElements[j].getAttribute != 'function') continue;
			if (myItemElements[j].getAttribute("myProperty") == "quantity") {
				var newItem=new cartItem();
				newItem.setFromArray(this);
				newItem.parentNode=this.parentNode;
				myItemElements[j].myItem=newItem;
				myItemElements[j].onclick=function(){
					this.myItem.quantity=-this.myItem.quantity;
					this.myItem.parentNode.addItem(this.myItem);
					this.myItem.parentNode.sumPrice();
					this.myItem.parentNode.refreshItemsListView(this.getAttribute("myFor"));
					return false; //This is necessary to avoid the link ordinary behavior
				}
				myItemElements[j].onmouseover=function(){
					this.innerHTML='X';
				}
				myItemElements[j].onmouseout=function(){
					this.innerHTML=this.myItem.quantity;
				}
			}
			// It modifies the href of the item name for adding it the item id that it is necessary for the destination href script to identify the product
			if (myItemElements[j].getAttribute("myProperty") == "name") {
				myItemElements[j].href=myItemElements[j].href+this.id;
				initLoadingElements(myItemElements[j]);
			}
		}
		
	};

/* myLoadItemsView
 * It defines the order object.
 * We have created this object like a complimet of the cart view for containing extra elements like subTotal and checkout buttons/links.
 * This way we can set the cart object view to a HTML table which rows are the items.
 * Order object view covers everything inside the shopping cart container including the buttons/links to perform the check out.
 * It has an added feature for encapsulating the entire object with it's indexed (cart and items) in a string that will be sent as a form field
 * to the check_out_1.php script/file for the order generation/recovering at the server side.
,*/


